﻿/*
 * OnlineAspParser - ASP.NET developer tool
 * Copyright (c) 2010, Bartłomiej Waluszko b.waluszko@hotmail.com
 * All Rights Reserved.
 * 
This file is part of OnlineAspParser.

OnlineAspParser is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

OnlineAspParser is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with OnlineAspParser; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Resources;
using System.Reflection;
using System.IO;

namespace OnlineAspParser {
    
    public partial class Parser : IHttpModule {
        private string indentString = "&nbsp;";


        public void Dispose() {

        }

        public void Init(HttpApplication application) {

            application.PreRequestHandlerExecute += (sender, e) => {
                IHttpHandler handler = application.Context.CurrentHandler;
                if (handler != null && handler is Page) 
                    ((Page)handler).Load += new EventHandler(currentPage_Load);
                
            };
        }

        void currentPage_Load(object sender, EventArgs e) {
            Page _page = (Page)sender;
            int i;

            //read css script from embedded resource
            string cssScript = "<style type=\"text/css\">\n";      
            Stream _stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("OnlineAspParser.style.css");
            _stream.ReadByte(); _stream.ReadByte(); _stream.ReadByte();
            while ((i = _stream.ReadByte()) != -1) 
                cssScript += (char)i;
            _stream.Close();
            cssScript += "\n</style>";

            //read javascript from embedded resource
            string javaScript = "<script type=\"text/javascript\">";
            _stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("OnlineAspParser.script.js");
            _stream.ReadByte(); _stream.ReadByte(); _stream.ReadByte();
            while ((i = _stream.ReadByte()) != -1)
                javaScript += (char)i;
            _stream.Close();
            javaScript += "</script>";
            
            //add JavaScript and CSS script to page
            ClientScriptManager clientScriptManager = _page.ClientScript;
            clientScriptManager.RegisterClientScriptBlock(this.GetType(), "JavaScriptScript", javaScript, false);
            clientScriptManager.RegisterClientScriptBlock(this.GetType(), "CssScript", cssScript, false);

            //small link to OnlineAspDebuger left bottom corner of screen
            HtmlGenericControl debugerMenu = new HtmlGenericControl("div");
            debugerMenu.ID = "OnlineAspParser_debugerMenu";
            debugerMenu.Attributes.Add("class", "OnlineAspParser_DebugerMenu");
            debugerMenu.Attributes.Add("onclick", "OnlineAspParser_ToggleDebugerVisibility(\'OnlineAspParser_debuger\');");
            debugerMenu.InnerHtml = "<b>OnlineAspParser</b>";

            //primary window of OnlineAspParser
            HtmlGenericControl debuger = new HtmlGenericControl("div");
            debuger.ID = "OnlineAspParser_debuger";
            debuger.Attributes.Add("class", "OnlineAspParser_Debuger");

            //add tabs 
            debuger.InnerHtml += "<div id=\"OnlineAspParser_tab1\" class=\"OnlineAspParser_Tab\" onclick=\"OnlineAspParser_ToggleTabsVisibility(this.id);\">List of ASP controls</div>";
            debuger.InnerHtml += "<div id=\"OnlineAspParser_tab2\" class=\"OnlineAspParser_Tab\" onclick=\"OnlineAspParser_ToggleTabsVisibility(this.id);\">Basic information</div>";
            debuger.InnerHtml += "<div id=\"OnlineAspParser_tab3\" class=\"OnlineAspParser_Tab\" onclick=\"OnlineAspParser_ToggleTabsVisibility(this.id);\">Server Variables</div>";
            debuger.InnerHtml += "<div id=\"OnlineAspParser_tab4\" class=\"OnlineAspParser_Tab\" onclick=\"OnlineAspParser_ToggleTabsVisibility(this.id);\">QueryString</div>";
            debuger.InnerHtml += "<div id=\"OnlineAspParser_tab5\" class=\"OnlineAspParser_Tab\" onclick=\"OnlineAspParser_ToggleTabsVisibility(this.id);\">Form</div>";
            debuger.InnerHtml += "<div id=\"OnlineAspParser_tab6\" class=\"OnlineAspParser_Tab\" onclick=\"OnlineAspParser_ToggleTabsVisibility(this.id);\">Cookies</div>";
            //debuger.InnerHtml += "<div id=\"OnlineAspParser_tab7\" class=\"OnlineAspParser_Tab\" onclick=\"OnlineAspParser_ToggleTabsVisibility(this.id);\">Empty</div>";
            debuger.InnerHtml += "<div id=\"OnlineAspParser_tab8\" class=\"OnlineAspParser_Tab\" onclick=\"OnlineAspParser_ToggleTabsVisibility(this.id);\">View State</div>";
            debuger.InnerHtml += "<div id=\"OnlineAspParser_tab9\" class=\"OnlineAspParser_Tab\" onclick=\"OnlineAspParser_ToggleTabsVisibility(this.id);\">About</div>";

            //asp controls 
            HtmlGenericControl divAspControls = new HtmlGenericControl("div");
            divAspControls.ID = "OnlineAspParser_tab1content";
            divAspControls.Attributes.Add("class", "OnlineAspParser_TabContent");
            divAspControls.InnerHtml = ParseWebControl(_page);
            debuger.Controls.Add(divAspControls);

            //Basic information
            HtmlGenericControl divBasicInformation = new HtmlGenericControl("div");
            divBasicInformation.ID = "OnlineAspParser_tab2content";
            divBasicInformation.Attributes.Add("class", "OnlineAspParser_TabContent");
            divBasicInformation.InnerHtml = PropertiesList(_page);
            debuger.Controls.Add(divBasicInformation);


            //ServerVariables
            HtmlGenericControl divServerVariables = new HtmlGenericControl("div");
            divServerVariables.ID = "OnlineAspParser_tab3content";
            divServerVariables.Attributes.Add("class", "OnlineAspParser_TabContent");
            divServerVariables.InnerHtml = GetNameValueCollection(_page.Request.ServerVariables);
            debuger.Controls.Add(divServerVariables);

            //QueryString
            HtmlGenericControl divQueryString = new HtmlGenericControl("div");
            divQueryString.ID = "OnlineAspParser_tab4content";
            divQueryString.Attributes.Add("class", "OnlineAspParser_TabContent");
            if (_page.Request.QueryString.Count > 0)
                divQueryString.InnerHtml = GetNameValueCollection(_page.Request.QueryString);
            else
                divQueryString.InnerHtml = "Query String Collection not found";
            debuger.Controls.Add(divQueryString);

            

            //Form
            HtmlGenericControl divForm = new HtmlGenericControl("div");
            divForm.ID = "OnlineAspParser_tab5content";
            divForm.Attributes.Add("class", "OnlineAspParser_TabContent");
            if (_page.Request.Form.Count > 0)
                divForm.InnerHtml = GetNameValueCollection(_page.Request.Form);
            else
                divForm.InnerHtml = "Form Collection not found";
            debuger.Controls.Add(divForm);

            //HttpCookieCollection
            HtmlGenericControl divCookies = new HtmlGenericControl("div");
            divCookies.ID = "OnlineAspParser_tab6content";
            divCookies.Attributes.Add("class", "OnlineAspParser_TabContent");
            if (_page.Request.Cookies.Count > 0)
                divCookies.InnerHtml = GetCookieCollection(_page.Request.Cookies);
            else
                divCookies.InnerHtml = "Cookies not found";
            debuger.Controls.Add(divCookies);

            //ViewState
            HtmlGenericControl divViewState = new HtmlGenericControl("div");
            divViewState.ID = "OnlineAspParser_tab8content";
            divViewState.Attributes.Add("class", "OnlineAspParser_TabContent");
            divViewState.InnerHtml = GetViewState(_page);
            debuger.Controls.Add(divViewState);


           


            //About info
            HtmlGenericControl divAbout = new HtmlGenericControl("div");
            divAbout.ID = "OnlineAspParser_tab9content";
            divAbout.Attributes.Add("class", "OnlineAspParser_TabContent");
            divAbout.InnerHtml = "OnlineAspParser ver.1.0<br/>";
            divAbout.InnerHtml += "License:  GNU General Public License version 2 (GPLv2)<br/>";
            divAbout.InnerHtml += "Author: Bartłomiej Waluszko<br/>Email: <u>b.waluszko @ hotmail.com</u><br/>";
            divAbout.InnerHtml += "Home page: <a href=\"http://onlineaspparser.codeplex.com\">www.onlineaspparser.codeplex.com</a><br/>";
            divAbout.InnerHtml += "I invite to exchange views and co-operation.";
            debuger.Controls.Add(divAbout);


            //Add divs & other controls to Page
            _page.Form.Controls.Add(debugerMenu);
            _page.Form.Controls.Add(debuger);
        }
    }
}
